//

// (  )
//

package com.tools.acceleratecore.xlib.tool.impl;

import android.app.Activity;
import android.app.KeyguardManager;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import com.tools.acceleratecore.xlib.XLibFactory;
import com.tools.acceleratecore.xlib.tool.intf.IDevicePolicyTool;

public class DevicePolicyTool implements IDevicePolicyTool {
    private Context mContext = null;
    private DevicePolicyManager mDevicePolicyManager = null;
    private ComponentName mComponentName = null;

    public DevicePolicyTool() {
        this.mContext = XLibFactory.getApplication();
        this._init();
    }

    private void _init() {
        this.mDevicePolicyManager = (DevicePolicyManager)this.mContext.getSystemService("device_policy");
    }

    public void setComponentName(ComponentName componentName) {
        this.mComponentName = componentName;
    }

    public boolean isActive() {
        return this.mComponentName == null ? false : this.mDevicePolicyManager.isAdminActive(this.mComponentName);
    }

    public void active(Activity activity) {
        if (activity != null && this.mComponentName != null) {
            Intent intent = new Intent("android.app.action.ADD_DEVICE_ADMIN");
            intent.putExtra("android.app.extra.DEVICE_ADMIN", this.mComponentName);
            intent.putExtra("android.app.extra.ADD_EXPLANATION", "Please active device admin to support lock");
            activity.startActivity(intent);
        }
    }

    public void unActive() {
        if (this.mComponentName != null) {
            this.mDevicePolicyManager.removeActiveAdmin(this.mComponentName);
        }
    }

    public void lockScreen() {
        this.mDevicePolicyManager.lockNow();
    }

    public void wipeData() {
        this.mDevicePolicyManager.wipeData(0);
    }

    public boolean isScreenOn() {
        PowerManager pm = (PowerManager)this.mContext.getSystemService("power");
        return pm.isScreenOn();
    }

    public boolean isScreenLock() {
        KeyguardManager km = (KeyguardManager)this.mContext.getSystemService("keyguard");
        return km.inKeyguardRestrictedInputMode();
    }
}
